package tw.com.hitevision.whiteboard.android.widgets;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import tw.com.hitevision.whiteboard.android.R;


public class OptionsRulerMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static String RULER = "ruler";
    public static String TRIANGLE_RULER_30 = "triangle ruler 30 degree";
    public static String TRIANGLE_RULER_45 = "triangle ruler 45 degree";
    public static String TRIANGLE_PROTRACTOR_RULER = "triangle protractor ruler";
    public static String PROTRACTOR_DEGREE_180 = "protractor_degree_180";
    public static String PROTRACTOR_DEGREE_360 = "protractor_degree_360";
    public static OptionsRulerMenuAdapter.OptionmRulerViewHolder mmrulerViewHolder;

    public static ArrayList<String> names;
    static ArrayList<Boolean> selectedList;
    public static Listener listener;
    @ColorInt
    public static int selectedColor;
    @ColorInt
    public static int unselectedColor;

    /*updated by zf*/
    public static iconListener miconListener;
    public static iconListener miconListener_ruler_30;
    public static iconListener miconListener_ruler_45;
    public static iconListener miconListener_RIANGLE_PROTRACTOR_RULER;
    public static iconListener miconListener_ROTRACTOR_DEGREE_180;
    public static iconListener miconListener_ROTRACTOR_DEGREE_360;

    public static List<View.OnClickListener> listenerList = new ArrayList<>();

    public static List<View.OnClickListener> getLister() {
        return listenerList;
    }

    /*updated by zf*/
    public static View.OnClickListener getLister(String name) {
        if (name == RULER) {
            return miconListener;
        }
        if (name == TRIANGLE_RULER_30) {
            return miconListener_ruler_30;
        }
        if (name == TRIANGLE_RULER_45) {
            return miconListener_ruler_45;
        }
        if (name == TRIANGLE_PROTRACTOR_RULER) {
            return miconListener_RIANGLE_PROTRACTOR_RULER;
        }
        if (name == PROTRACTOR_DEGREE_180) {
            return miconListener_ROTRACTOR_DEGREE_180;
        }
        if (name == PROTRACTOR_DEGREE_360) {
            return miconListener_ROTRACTOR_DEGREE_360;
        }
        return null;
    }

    public OptionsRulerMenuAdapter(Context context, Listener listener) {
        this.listener = listener;

        names = new ArrayList<>();
        names.add(RULER);
        names.add(TRIANGLE_RULER_45);
        names.add(TRIANGLE_RULER_30);
        names.add(PROTRACTOR_DEGREE_180);
        names.add(PROTRACTOR_DEGREE_360);
        names.add(TRIANGLE_PROTRACTOR_RULER);

        selectedList = new ArrayList<>();
        for (int i = 0; i < names.size(); i++) {
            selectedList.add(false);
        }

        selectedColor = ContextCompat.getColor(context, R.color.hitevision_blue);
        unselectedColor = ContextCompat.getColor(context, R.color.transparent);
    }

    public static class OptionmRulerViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;

        public OptionmRulerViewHolder(View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.tool_icon);
        }
    }

    public void setSelectedList(ArrayList<Boolean> selectedList) {
        this.selectedList = selectedList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.options_ruler_menu_child_layout, parent, false);
        return new OptionmRulerViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        OptionmRulerViewHolder mrulerViewHolder = (OptionmRulerViewHolder) holder;
        mmrulerViewHolder = (OptionmRulerViewHolder) holder;
        switch (position) {
            case 0: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_ruler);
                break;
            }
            case 1: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_triangle_45);
                break;
            }
            case 2: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_triangle_3060);
                break;
            }
            case 3: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_protractor_180);
                break;
            }
            case 4: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_protractor_360);
                break;
            }
            case 5: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_triangle_protractor);
                break;
            }
        }

        switch (position) {
            case 0: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_ruler);
                break;
            }
            case 1: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_triangle_45);
                break;
            }
            case 2: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_triangle_3060);
                break;
            }
            case 3: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_protractor_180);
                break;
            }
            case 4: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_protractor_360);
                break;
            }
            case 5: {
                mrulerViewHolder.icon.setImageResource(R.drawable.ic_tools_rulers_triangle_protractor);
                break;
            }
        }

        if (position >= 0 && position < selectedList.size()) {
            mrulerViewHolder.itemView.setSelected(selectedList.get(position));
            mrulerViewHolder.icon.setColorFilter(selectedList.get(position) ? selectedColor : unselectedColor);
        }



        /*updated by zf*/

        if (position == 0) {
            OptionmRulerViewHolder mrulerViewHolder0 = (OptionmRulerViewHolder) holder;
            miconListener = new iconListener(mrulerViewHolder, position);
            mrulerViewHolder0.itemView.setOnClickListener(miconListener);

        }
        if (position == 1) {
            OptionmRulerViewHolder mrulerViewHolder1 = (OptionmRulerViewHolder) holder;
            miconListener_ruler_45 = new iconListener(mrulerViewHolder, position);
            mrulerViewHolder1.itemView.setOnClickListener(miconListener_ruler_45);

        }
        if (position == 2) {
            OptionmRulerViewHolder mrulerViewHolder2 = (OptionmRulerViewHolder) holder;
            miconListener_ruler_30 = new iconListener(mrulerViewHolder, position);
            mrulerViewHolder2.itemView.setOnClickListener(miconListener_ruler_30);

        }
        if (position == 3) {
            OptionmRulerViewHolder mrulerViewHolder3 = (OptionmRulerViewHolder) holder;
            miconListener_ROTRACTOR_DEGREE_180 = new iconListener(mrulerViewHolder, position);
            mrulerViewHolder3.itemView.setOnClickListener(miconListener_ROTRACTOR_DEGREE_180);

        }
        if (position == 4) {
            OptionmRulerViewHolder mrulerViewHolder4 = (OptionmRulerViewHolder) holder;
            miconListener_ROTRACTOR_DEGREE_360 = new iconListener(mrulerViewHolder, position);
            mrulerViewHolder4.itemView.setOnClickListener(miconListener_ROTRACTOR_DEGREE_360);

        }
        if (position == 5) {
            OptionmRulerViewHolder mrulerViewHolder5 = (OptionmRulerViewHolder) holder;
            miconListener_RIANGLE_PROTRACTOR_RULER = new iconListener(mrulerViewHolder, position);
            mrulerViewHolder5.itemView.setOnClickListener(miconListener_RIANGLE_PROTRACTOR_RULER);

        }

//        mrulerViewHolder.itemView.setOnClickListener(v -> {
//            mrulerViewHolder.itemView.setSelected(!mrulerViewHolder.itemView.isSelected());
//            mrulerViewHolder.icon.setColorFilter(mrulerViewHolder.itemView.isSelected() ? selectedColor : unselectedColor);
//            if (position >= 0 && position < selectedList.size()) {
//                selectedList.set(position, mrulerViewHolder.itemView.isSelected());
//            }
//            if (listener != null) {
//                listener.onItemClick(names.get(position));
//            }
//        });
    }


    @Override
    public int getItemCount() {
        return names.size();
    }

    public interface Listener {

        void onItemClick(String name);

    }

}

/*updated by zf*/
class iconListener implements View.OnClickListener {
    private OptionsRulerMenuAdapter.OptionmRulerViewHolder mmmrulerViewHolder;
    private int position;

    @Override
    public void onClick(View v) {

        if (mmmrulerViewHolder != null) {
            if (mmmrulerViewHolder.itemView != null) {
                mmmrulerViewHolder.itemView.setSelected(!mmmrulerViewHolder.itemView.isSelected());
            } else {

            }
            if (mmmrulerViewHolder.icon != null) {
                mmmrulerViewHolder.icon.setColorFilter(mmmrulerViewHolder.itemView.isSelected() ? OptionsRulerMenuAdapter.selectedColor : OptionsRulerMenuAdapter.unselectedColor);
            } else {

            }
            if (position >= 0 && position < OptionsRulerMenuAdapter.selectedList.size()) {
                OptionsRulerMenuAdapter.selectedList.set(position, mmmrulerViewHolder.itemView.isSelected());
            }
            if (OptionsRulerMenuAdapter.listener != null) {
                OptionsRulerMenuAdapter.listener.onItemClick(OptionsRulerMenuAdapter.names.get(position));
            }
        } else {

        }
    }

    public iconListener(OptionsRulerMenuAdapter.OptionmRulerViewHolder mrulerViewHolder, int position) {

        mmmrulerViewHolder = mrulerViewHolder;
        this.position = position;
    }

}
